$(function () {
    var show_tip=function (type,mssage) {
        var myAlert=$("<div></div>");
       var $form=$(document.body.querySelectorAll("form")).append(myAlert);
       if(type=="success"){
           myAlert.addClass("alert alert-success").append('<a href="#" class="close" data-dismiss="alert">&times;</a> <strong>'+mssage+'</strong>。');
       }
        if(type=="error")
        {
            myAlert.addClass("alert alert-warning").append('<a href="#" class="close" data-dismiss="alert">&times;</a> <strong>'+mssage+'</strong>。');
        }
        this.hide=function () {
            myAlert.empty();
        }

    }
    $("#regist").click(function () {
        var username = $("#username").val().trim();
        if(!username || username==null || username==""){
            $("#username").css("border", "1px solid red");
            return;
        }
        var password = $("#password").val();
        var vepassword = $("#vepassword").val();
        var sex=$("#sex").select().find("option:selected").text();
        if (password !== vepassword) {
            $("#password").css("border", "1px solid red");
            $("#vepassword").css("border", "1px solid red");
            return;
        }
        else if (password === vepassword) {
            var data = {"uname": username, "upwd": password,"usex":sex};
            $.ajax({
                url: '/register/reg',
                type: 'post',
                data: data,
                success: function (data, status) {
                    if (data.success) {
                        show_tip("success",data.message);
                        setTimeout(function () {
                            location.href = '/login';
                        },3000);
                    }
                    else{
                        show_tip("error",data.message);
                        setTimeout(function () {
                            location.href='/register'
                        },3000);

                    }


                },
                error: function (data, err) {
                    location.href = 'register';
                },
                complete:function (XHR, TS) {
                    $("#username").val("");
                    $("#password").val("");
                    $("#vepassword").val("");

                }
            });
        }
    });
    $("#login").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        if (username == "") {
            $("#username").css("border", "1px solid red");
            $(".alert").alert("帐号密码错误");
            return;
        }
        else if (password == "") {
            $("#password").css("border", "1px solid red");
            return;
        }
        var data = {"uname": username, "upwd": password};
        $.ajax({
            url: '/login/log',
            type: 'post',
            data: data,
            success: function (data, status) {
                if (data.success) {
                    location.href = '/';
                }
                else
                    {
                    show_tip("error",data.message);
                    setTimeout(function () {
                        location.href = 'login';
                    },3000);

                }
            },
            error: function (data, err) {
                location.href = 'login';
            },
            complete:function (XHR, TS) {
              $("#username").val("");
              $("#password").val("");
               $("#vepassword").val("");

            }
        });


    })
})